<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

global $USER, $CFG, $COURSE;
    require_once('../config.php');
$sql = "SELECT
                post.id,
                post.user_id,
                post.courseid,
                post.id_Section,
                pc.dequien,
                pc.paraquien,
                pc.body,
                pc.stamp
            FROM
                mdl_rsa_post AS post ,
                mdl_rsa_post_comentario AS pc
            WHERE
                post.visible = 1 AND
                post.courseid = 34394 AND
                post.id_Section = 1263 AND
                post.id = pc.post_id AND
                pc.visible = 1 AND
                pc.courseid = 34394";
    
    $comentarios = get_records_sql($sql);
    
    if( $comentarios ){
        $coment = '<div class="rsa">
                        <ul data-role="listview" data-divider-theme="b" data-inset="true" data-count-theme="e"> 
                            <li data-role="list-divider">Comentarios</p></li>';
        foreach( $comentarios as $comentario){                                     
                $coment .= '<li>                                                             
                                <p>
                                    <img src="'.$CFG->wwwroot.'/user/pix.php?file='.$comentario->dequien.'/f1.jpg"  alt="Estudiante picture">
                                   '.  get_nombres($comentario->dequien) .'
                                   <p>'.$comentario->body.'</p>
                                </p>                                    
                                <span class="ui-li-count">'.  get_fecha($comentario->stamp) .'</span>
                            </li>';                                        
        }//end foreach
                $coment .= '<li id="coments_btn">  
                                    
                                <p>
                                    <img src="'.$CFG->wwwroot.'/user/pix.php?file='.$USER->id.'/f1.jpg"  alt="Estudiante picture">
                                    <p>'.  get_nombres($USER->id) .'</p>  
                                    <textarea id="coment" name="coment" placeholder=""></textarea>
                                </p> 
                                <div data-role="controlgroup" >
                                  <a id="postid_'.$comentario->id.'" class="coments_btn_rsa" href="#" data-theme="b"  data-role="button">Aceptar</a>
                                  <a href="#"  data-role="button">Cancelar</a>
                                </div>
                            </li>'; 
                
             $coment .= ' </ul>
                     </div>';
             echo  $coment;
    }else{
        echo " false";
    }
    
    function get_nombres($userid){
    $usuario = get_record("user","id",$userid);
    
    if($usuario){
        $name = utf8_decode("$usuario->firstname $usuario->lastname");
        return "<h1>$name</h1>"; 
    }else{
        return "";
    }
    
}

function get_fecha( $timestamp ){
    $dias= array ("Domingo","Lunes", "Martes", "Miércoles","Jueves", "Viernes", "Sábado");
    $meses = array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
    $dia_text = utf8_decode($dias[date("N",$timestamp)]);
    $mes_text = utf8_decode($meses[ date("m",$timestamp) * 1 ]);
    //return "$dia_text, ".date("j",$timestamp). " de (".date("m",$timestamp) .")  de ".date("o",$timestamp). " " . date("H:m",$timestamp) ;
    return "$dia_text, ".date("j",$timestamp). " de $mes_text de ".date("o",$timestamp). " " . date("H:m",$timestamp) ;
}
?>
